package com.lk.v1;

import com.lk.base.service.ConfigService;
import com.lk.base.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

@Controller
public class LoginController {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ConfigService configService;


    @PostMapping("/login")
    @ResponseBody
    public Map<String,Object> login(String username, String password , HttpSession session) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        if(configService.getLoginUsername().equals(username) && configService.getLoginPassword().equals(password)){
            session.setAttribute("users", username);
            return ResultUtil.Ok("登录成功");
        }else{
            logger.error("用户名或密码错误");
            return ResultUtil.Error("用户名或密码错误");
        }
    }

    @RequestMapping("/login")
    public String tologin()
    {
        return "login";
    }
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute("users"); //退出登录则清除session中的用户信息
        return "redirect:/login";
    }


}
